﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Newtonsoft.Json.Linq;
using WeiboPlus.Common;
using WeiboPlus.Models;
using WeiboPlus.Service;

namespace WeiboPlus.Sina
{
    public class SinaJTokenToStatusMapping : MappingBase<JToken, SNSStatus>
    {
        public SinaJTokenToStatusMapping(IMappingFactory factory)
            : base(factory)
        {

        }

        public override SNSStatus Map(JToken s)
        {
            try
            {
                var status = new SNSStatus()
                {
                    Account = _factory.Account,
                    Provider = SNSProviderType.Sina
                };
                if (s.SelectToken("created_at") != null)
                {
                    var str = (string)s.SelectToken("created_at");
                    if (!string.IsNullOrEmpty(str))
                        status.CreatedTime = DateTime.ParseExact((string)s.SelectToken("created_at"), "ddd MMM dd HH:mm:ss zzzz yyyy", new CultureInfo("en-us"));
                }
                if (s.SelectToken("text") != null)
                    status.Text = (string)s.SelectToken("text");
                status.Id = (long)s.SelectToken("id");
                if (s.SelectToken("source") != null)
                {
                    var source = (string)s.SelectToken("source");

                    if (source.Contains("</a>"))
                    {
                        source = source.TrimEnd(new char[] { '<', '/', 'a', '>' });
                        source = source.Substring(source.LastIndexOf('>') + 1);
                    }
                    status.Source = source;
                }
                if (s.SelectToken("user") != null && s.SelectToken("user").HasValues)
                    status.User = _factory.GetMapping<JToken, SNSUser>().Map(s.SelectToken("user"));

                if (s.SelectToken("status") != null && s.SelectToken("status").HasValues)
                {
                    status.Type = StatusType.Comment;
                    goto LABEL_COMMENT;
                }

                if (s.SelectToken("retweeted_status") != null && s.SelectToken("retweeted_status").HasValues)
                {
                    status.Type = StatusType.Repost;
                    status.Prefix = AppResx.RepostPrifix;

                    goto LABEL_REPOST;
                }

                status.Type = StatusType.Original;
                goto LABEL_ORIGINAL;


            LABEL_COMMENT:
                status.RetweetedStatus = _factory.GetMapping<JToken, SNSStatus>().Map(s.SelectToken("status"));
                goto LABEL_RETURN;
            LABEL_REPOST:
                status.RetweetedStatus = _factory.GetMapping<JToken, SNSStatus>().Map(s.SelectToken("retweeted_status"));

            //LABEL_REPLY:
            LABEL_ORIGINAL:
                //LABEL_DERECTMESSAGE:
                if (s.SelectToken("favorited") != null)
                    status.Favorited = (bool)s.SelectToken("favorited");
                if (s.SelectToken("in_reply_to_screen_name") != null)
                    status.ReplyToScreenName = (string)s.SelectToken("in_reply_to_screen_name");
                if (s.SelectToken("in_reply_to_status_id") != null)
                    status.ReplyToStatusId = (string)s.SelectToken("in_reply_to_status_id");
                if (s.SelectToken("in_reply_to_user_id") != null)
                    status.ReplyToUserId = (string)s.SelectToken("in_reply_to_user_id");

                //if (s.SelectToken("retweeted_status") != null && s.SelectToken("retweeted_status").HasValues)
                //    status.RetweetedStatus = _factory.GetMapping<JToken, SNSStatus>().Map(s.SelectToken("retweeted_status"));
                if (s.SelectToken("thumbnail_pic") != null)
                    status.ThumbnailPic = (string)s.SelectToken("thumbnail_pic");
                if (s.SelectToken("bmiddle_pic") != null)
                    status.BmiddlePic = (string)s.SelectToken("bmiddle_pic");
                if (s.SelectToken("original_pic") != null)
                    status.OriginalPic = (string)s.SelectToken("original_pic");
                if (s.SelectToken("reposts_count") != null)
                    status.RepostCount = (int)s.SelectToken("reposts_count");
                if (s.SelectToken("comments_count") != null)
                    status.CommentCount = (int)s.SelectToken("comments_count");

            LABEL_RETURN:

                return status;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return null;
                //throw;
            }
        }
    }
}
